package gsw.demo.mvp.core.net.rxservice;

import java.util.concurrent.TimeUnit;

import gsw.demo.mvp.core.net.config.ConfigInit;
import gsw.demo.mvp.core.net.config.ConfigKeys;
import okhttp3.OkHttpClient;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.scalars.ScalarsConverterFactory;

/**
 * RestService的创建者
 */
public final class RxRestCreator {

    /**
     * 产生一个全局的Retrofit客户端
     */
    private static final class RetrofitHolder {
        private static final String BASE_URL = ConfigInit.getConfiguration(ConfigKeys.HOST);
        private static final Retrofit RETROFIT_CLIENT = new Retrofit.Builder()
                .baseUrl(BASE_URL)
                .addConverterFactory(ScalarsConverterFactory.create())
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .client(OKHttpHolder.OK_HTTP_CLIENT)
                .build();
    }

    /**
     * 产生一个全局的OkHttpClient
     */
    private static final class OKHttpHolder {
        private static final int TIME_OUT = 60;
        private static final OkHttpClient OK_HTTP_CLIENT = new OkHttpClient.Builder().connectTimeout(TIME_OUT, TimeUnit.SECONDS).build();
    }

    /**
     * 产生一个全局的RestService
     */
    private static final class RestServiceHolder {
        private static final RxRestService REST_SERVICE = RetrofitHolder.RETROFIT_CLIENT.create(RxRestService.class);
    }

    /**
     * 获取RestService
     */
    public static RxRestService getRestService() {
        return RestServiceHolder.REST_SERVICE;
    }
}








